Wednesday, 17 November 2010

Java Mail

Hi , This is small example by which u can send mail using Java mail.

The libraries that have been used is as follow

activation.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
mail.jar


public static void sendMail(
String from
, String torecp
, String Subject
, String greeting
, String mailContent
, String signature
, String ccRecp
, String bccRecp) throws AddressException, MessagingException {
System.out.println("reading the pdfFile......");

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "XXXXXX");
props.setProperty("mail.user", "someUser@xx.com");
props.setProperty("mail.password", "password");

System.out.println("Creating the mail...");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(Subject);
message.setFrom(new InternetAddress(from));

addRecipient(torecp, message,Message.RecipientType.TO);
addRecipient(bccRecp, message,Message.RecipientType.BCC);//to send mails in BCC list
addRecipient(ccRecp, message,Message.RecipientType.CC);


// This HTML mail have to 2 part, the BODY and the embedded image
//
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "
" + greeting + "

";
htmlText = htmlText + "
" + mailContent + "

";


// String imgText = "";
int j=0;

htmlText = htmlText + "
" + signature + "

";
messageBodyPart.setContent(htmlText, "text/html");
System.out.println("Adding the image.....");
// add it
multipart.addBodyPart(messageBodyPart);
int i=0;


// put everything together
message.setContent(multipart);

// add the Multipart to the message
message.setContent(multipart);
System.out.println("connecting to the SMTP server....");
//connecting to the SMTP server....
transport.connect();
System.out.println("Sending the mail.....");
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));

transport.close();
System.out.println("Mail sent successfully.....");
}



/**
* @param recipientAddressStr
* @param message
* @throws AddressException
* @throws MessagingException
*/
private static void addRecipient(String recipientAddressStr,
MimeMessage message, RecipientType recpientType) throws AddressException, MessagingException {
StringTokenizer st = new StringTokenizer(recipientAddressStr,";");
int countTokens = st.countTokens();


List bccList = new ArrayList();

if (countTokens >0 ) {
while(st.hasMoreTokens()){
String s = st.nextToken();
s= s.trim();
if (s != null && !("").equals(s)) {
System.out.println(" Adding Recipient to CC:" + s);
bccList.add(s);
}
}

Address[] toAddr = new Address[bccList.size()];
int k=0;
for (String string : bccList) {
toAddr[k] = new InternetAddress(string);
k++;
}
message.addRecipients(recpientType, toAddr);
} else {
if (recipientAddressStr != null && !("").equals(recipientAddressStr) && !recipientAddressStr.trim().equals("")) {
message.addRecipient(recpientType
, new InternetAddress(recipientAddressStr));
}
}
}


If u need any furthur clarification u can contact me through vim.vimal@gmail.com

Sunday, 30 August 2009